package com.gjs.management.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gjs.common.exception.BizException;
import com.gjs.management.mapper.SysRolePermissionMapper;
import com.gjs.management.pojo.entity.SysPermission;
import com.gjs.management.pojo.entity.SysRole;
import com.gjs.management.pojo.entity.SysRolePermission;
import com.gjs.management.service.ISysPermissionService;
import com.gjs.management.service.ISysRolePermissionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class SysRolePermissionServiceImpl extends ServiceImpl<SysRolePermissionMapper, SysRolePermission> implements ISysRolePermissionService {

    private final ISysPermissionService permissionService;

    @Override
    public List<SysPermission> getPermissionListByRoleId(Long id) {
        Set<Long> permissionIdSet = this.getBaseMapper()
                .selectObjs(new LambdaQueryWrapper<SysRolePermission>().select(SysRolePermission::getPermissionId)).stream()
                .map(item -> Long.parseLong(item.toString()))
                .collect(Collectors.toSet());
        return permissionIdSet.isEmpty() ? null : permissionService.list(new LambdaQueryWrapper<SysPermission>().in(SysPermission::getId, permissionIdSet));
    }

    @Override
    public Boolean saveByPermissionKeySet(SysRole role, Set<String> permissionKeySet) {
        List<SysPermission> sysPermissionList = permissionService.list(new LambdaQueryWrapper<SysPermission>().in(SysPermission::getPermissionKey, permissionKeySet));
        if (sysPermissionList.size() != permissionKeySet.size()) {
            throw new BizException("部分权限关键字不存在");
        }

        // 先清除被删除了的权限
        Set<Long> sysPermissionIdSet = sysPermissionList.stream().map(SysPermission::getId).collect(Collectors.toSet());
        LambdaQueryWrapper<SysRolePermission> deleteWrapper = new LambdaQueryWrapper<>();
        deleteWrapper.eq(SysRolePermission::getRoleId, role.getId());
        deleteWrapper.notIn(SysRolePermission::getPermissionId, sysPermissionIdSet);
        this.getBaseMapper().delete(deleteWrapper);

        // 求新旧权限之间的差集
        Set<Long> oldSysPermissionIdSet = this.getBaseMapper()
                .selectObjs(new LambdaQueryWrapper<SysRolePermission>().select(SysRolePermission::getPermissionId)).stream()
                .map(item -> Long.parseLong(item.toString()))
                .collect(Collectors.toSet());
        sysPermissionIdSet.removeAll(oldSysPermissionIdSet);

        for (SysPermission permission : sysPermissionList) {
            // 已存在的权限就跳过咯
            if (!sysPermissionIdSet.contains(permission.getId())) {
                continue;
            }
            SysRolePermission sysRolePermission = new SysRolePermission();
            sysRolePermission.setPermissionId(permission.getId());
            sysRolePermission.setRoleId(role.getId());
            // 保存角色权限
            boolean res = this.save(sysRolePermission);
            if (!res) {
                throw new BizException("写入角色权限失败");
            }
        }

        return true;
    }
}
